package org.swingDemo.UI;

import com.esotericsoftware.tablelayout.swing.Table;
import org.swingDemo.helper.ClassifyOptionHelper;
import org.swingDemo.helper.StringHelper;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ClassifyOptionPanel extends JPanel implements ActionListener, DocumentListener {

    private JCheckBox recursiveSubDirCheckBox;
    private JCheckBox persistSubDirCheckBox;
    private JRadioButton copyRadioButton;
    private JRadioButton moveRadioButton;
    private JTextField extensionTextField;
    private JTextField folderMapTextField;

    public ClassifyOptionPanel() {
        super(new BorderLayout());

        initPanel();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == recursiveSubDirCheckBox) {
            ClassifyOptionHelper.setRecursiveSubDir(recursiveSubDirCheckBox.isSelected());
        } else if (source == persistSubDirCheckBox) {
            ClassifyOptionHelper.setPersistSubDir(persistSubDirCheckBox.isSelected());
        } else if (source == copyRadioButton || source == moveRadioButton) {
            ClassifyOptionHelper.setCopyOrMove(copyRadioButton.isSelected());
        }
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        updateClassifyOption();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        updateClassifyOption();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        updateClassifyOption();
    }

    private void initPanel() {
        recursiveSubDirCheckBox = new JCheckBox(StringHelper.RECURSIVE_SUB_DIR);
        recursiveSubDirCheckBox.addActionListener(this);
        recursiveSubDirCheckBox.setSelected(ClassifyOptionHelper.isRecursiveSubDir());

        persistSubDirCheckBox = new JCheckBox(StringHelper.PERSIST_SUB_DIR);
        persistSubDirCheckBox.addActionListener(this);
        persistSubDirCheckBox.setSelected(ClassifyOptionHelper.isPersistSubDir());

        copyRadioButton = new JRadioButton(StringHelper.COPY_FILE);
        copyRadioButton.addActionListener(this);
        copyRadioButton.setSelected(ClassifyOptionHelper.isCopyOrMove());

        moveRadioButton = new JRadioButton(StringHelper.MOVE_FILE);
        moveRadioButton.addActionListener(this);
        moveRadioButton.setSelected(!ClassifyOptionHelper.isCopyOrMove());

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(copyRadioButton);
        buttonGroup.add(moveRadioButton);

        extensionTextField = new JTextField();
        extensionTextField.getDocument().addDocumentListener(this);
        extensionTextField.setText(ClassifyOptionHelper.getFileExtension());

        folderMapTextField = new JTextField();
        folderMapTextField.getDocument().addDocumentListener(this);
        folderMapTextField.setText(ClassifyOptionHelper.getFolderMap());

        Table table = new Table();
        table.defaults().left();
        table.addCell(recursiveSubDirCheckBox);
        table.addCell(StringHelper.COMPONENT_SEPARATOR);
        table.addCell(persistSubDirCheckBox);
        table.addCell(StringHelper.COMPONENT_SEPARATOR);
        table.addCell(copyRadioButton);
        table.addCell(moveRadioButton);
        table.addCell(StringHelper.COMPONENT_SEPARATOR);
        table.addCell(StringHelper.FILE_EXTENSION);
        table.addCell(extensionTextField).expandX().fillX();

        Table table1 = new Table();
        table1.addCell(StringHelper.FOLDER_MAP);
        table1.addCell(folderMapTextField).colspan(5).expandX().fillX();

        this.setBorder(BorderFactory.createTitledBorder(StringHelper.CLASSIFY_OPTION));
        add(table);
        add(table1, BorderLayout.SOUTH);
    }

    private void updateClassifyOption() {
        if (extensionTextField != null) {
            ClassifyOptionHelper.setFileExtension(extensionTextField.getText().trim());
        }
        if (folderMapTextField != null) {
            ClassifyOptionHelper.setFolderMap(folderMapTextField.getText().trim());
        }
    }
}
